package com.juc.lock;

import java.util.concurrent.TimeUnit;

/**
 * @Author Zan
 * @Create 2024/5/7 21:17
 * @ClassName : DeadLockDemo
 * @Description : TODO 请用一句话描述该类的功能
 */
public class DeadLockDemo {
    public static void main(String[] args) {

        final Object objA = new Object();
        final Object objB = new Object();

        new Thread(() -> {
            synchronized (objA) {
                System.out.println(Thread.currentThread().getName() + "\t 自己持有A锁，希望获得B锁");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (objB) {
                    System.out.println(Thread.currentThread().getName() + "\t 成功获得B锁");
                }
            }
        }, "A").start();

        new Thread(() -> {
            synchronized (objB) {
                System.out.println(Thread.currentThread().getName() + "\t 自己持有B锁，希望获得A锁");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (objA) {
                    System.out.println(Thread.currentThread().getName() + "\t 成功获得A锁");
                }
            }
        }, "B").start();
    }
}
